package com.ibm.iocc67;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;

import javax.portlet.*;

import DAO.ioc_thongtintiepnhanDAO;
import Data.IocThongtintiepnhan;


/**
 * A sample portlet based on GenericPortlet
 */
public class Thongtin_tiepnhanPortlet extends GenericPortlet {

	public static final String JSP_FOLDER    = "/_thongtin_tiepnhan/jsp/";    // JSP folder name

	public static final String VIEW_JSP      = "Thongtin_tiepnhanPortletView";         // JSP file name to be rendered on the view mode
	public static final String SESSION_BEAN  = "Thongtin_tiepnhanPortletSessionBean";  // Bean name for the portlet session
	public static final String FORM_SUBMIT   = "Thongtin_tiepnhanPortletFormSubmit";   // Action name for submit form
	public static final String FORM_TEXT     = "Thongtin_tiepnhanPortletFormText";     // Parameter name for the text input



	 
	/**
	 * @see javax.portlet.Portlet#init()
	 */
	public void init() throws PortletException{
		super.init();
	}

	/**
	 * Serve up the <code>view</code> mode.
	 * 
	 * @see javax.portlet.GenericPortlet#doView(javax.portlet.RenderRequest, javax.portlet.RenderResponse)
	 */
	public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {
		// Set the MIME type for the render response
		response.setContentType(request.getResponseContentType());

		// Check if portlet session exists
		Thongtin_tiepnhanPortletSessionBean sessionBean = getSessionBean(request);
		if( sessionBean==null ) {
			response.getWriter().println("<b>NO PORTLET SESSION YET</b>");
			return;
		}

		// Invoke the JSP to render
		PortletRequestDispatcher rd = getPortletContext().getRequestDispatcher(getJspFilePath(request, VIEW_JSP));
		rd.include(request,response);
	}

	/**
	 * Process an action request.
	 * 
	 * @see javax.portlet.Portlet#processAction(javax.portlet.ActionRequest, javax.portlet.ActionResponse)
	 */
	public void processAction(ActionRequest request, ActionResponse response) throws PortletException, java.io.IOException {
		if( request.getParameter(FORM_SUBMIT) != null ) {
			// Set form text in the session bean
			Thongtin_tiepnhanPortletSessionBean sessionBean = getSessionBean(request);
			if( sessionBean != null )
				sessionBean.setFormText(request.getParameter(FORM_TEXT));
			String command = request.getParameter("command");
			int id = 0;
			
			int gid=0;
			int nguoi_gui = 0;
			String ten_lienhe = null;
			String email_lienhe= null;
			String dienthoai_lienhe= null;
			int loai_sukien=0;
			int trangthai_sukien=0;
			int phamvi_sukien=0;
			String han_che= null;
			String tieu_de= null;
			int mucdo_nghiemtrong=0;
			int mucdo_chacchan=0;
			int mucdo_khancap=0;
			//int loai_tin= Integer.parseInt(request.getParameter("loai_tin"));
			String mo_ta= null;
			Date ngay_gui= null;
			Date thoidiem_gui= null;
			Date ngay_hieuluc= null;
			Date thoidiem_hieuluc= null;
			Date ngay_batdau= null;
			Date thoidiem_batdau= null;
			Date ngay_ketthuc= null;
			Date thoidiem_ketthuc= null;
			String mota_khuvuc= null;
			int tinh=0;
			int huyen=0;
			String longitude=null;
			String lat=null;
			//
			
			
			id = Integer.parseInt(request.getParameter("id"));
			if(!command.equals("delete")){
			gid = Integer.parseInt(request.getParameter("gid"));
			nguoi_gui = Integer.parseInt(request.getParameter("nguoi_gui"));
			ten_lienhe = request.getParameter("ten_lienhe");
			email_lienhe = request.getParameter("email_lienhe");
			dienthoai_lienhe = (request.getParameter("dienthoai_lienhe"));
			loai_sukien= Integer.parseInt(request.getParameter("slloai_sukien"));
			trangthai_sukien= Integer.parseInt(request.getParameter("sltrangthai_sukien"));
			phamvi_sukien= Integer.parseInt(request.getParameter("slphamvi_sukien"));
			han_che = request.getParameter("han_che");
			tieu_de= request.getParameter("tieu_de");
			mucdo_nghiemtrong= Integer.parseInt(request.getParameter("slmucdo_nghiemtrong"));
			mucdo_chacchan= Integer.parseInt(request.getParameter("slmucdo_chacchan"));
			mucdo_khancap= Integer.parseInt(request.getParameter("slmucdo_khancap"));
			//int loai_tin= Integer.parseInt(request.getParameter("loai_tin"));
			mo_ta= request.getParameter("mo_ta");
			ngay_gui= convertStringToDate(request.getParameter("ngay_gui"));
			thoidiem_gui= convertStringToTime(request.getParameter("thoidiem_gui"));
			ngay_hieuluc= convertStringToDate(request.getParameter("ngay_hieuluc"));
			thoidiem_hieuluc= convertStringToTime(request.getParameter("thoidiem_hieuluc"));
			ngay_batdau= convertStringToDate(request.getParameter("ngay_batdau"));
			thoidiem_batdau= convertStringToTime(request.getParameter("thoidiem_batdau"));
			ngay_ketthuc= convertStringToDate(request.getParameter("ngay_ketthuc"));
			thoidiem_ketthuc= convertStringToTime(request.getParameter("thoidiem_ketthuc"));
			mota_khuvuc= (request.getParameter("mota_khuvuc"));
			tinh= Integer.parseInt(request.getParameter("slTenTinh"));
			huyen= Integer.parseInt(request.getParameter("slHuyen"));
			longitude = request.getParameter("longitude");
			lat = request.getParameter("latitude");
			//int xa= Integer.parseInt(request.getParameter("xa"));
			}
			
			IocThongtintiepnhan obj = new IocThongtintiepnhan();
			obj.setId(id);
			obj.setNguoiGui(nguoi_gui);
			obj.setTenLienhe(ten_lienhe);
			obj.setEmailLienhe(email_lienhe);
			obj.setDienthoaiLienhe(dienthoai_lienhe);
			obj.setLoaiSukien(loai_sukien);
			obj.setTrangthaiSukien(trangthai_sukien);
			obj.setPhamviSukien(phamvi_sukien);
			obj.setHanChe(han_che);
			obj.setTieuDe(tieu_de);
			obj.setMucdoNghiemtrong(mucdo_nghiemtrong);
			obj.setMucdoChacchan(mucdo_chacchan);
			obj.setMucdoKhancap(mucdo_khancap);
			obj.setMoTa(mo_ta);
			obj.setNgayGui(ngay_gui);
			obj.setThoidiemGui(thoidiem_gui);
			obj.setNgayHieuluc(ngay_hieuluc);
			obj.setThoidiemHieuluc(thoidiem_hieuluc);
			obj.setNgayBatdau(ngay_batdau);
			obj.setThoidiemBatdau(thoidiem_batdau);
			obj.setNgayKetthuc(ngay_ketthuc);
			obj.setThoidiemKetthuc(thoidiem_ketthuc);
			obj.setMaTinh(tinh);
			obj.setMaHuyen(huyen);
			obj.setGid(gid);
			//obj.setMaXa(xa);
			obj.setMotaKhuvuc(mota_khuvuc);
			ioc_thongtintiepnhanDAO objDAO = new ioc_thongtintiepnhanDAO();
			if(command.equals("add")){
				boolean check = objDAO.save(obj);
				if(check){
					//JOptionPane.showMessageDialog(null, "Insert complete");
				}else{
					//JOptionPane.showMessageDialog(null, "Insert fail");
				}
			}
			if(command.equals("edit")){
				boolean check = objDAO.edit(obj);
				if(check){
					//JOptionPane.showMessageDialog(null, "Edit complete");
				}else{
					//JOptionPane.showMessageDialog(null, "Edit fail");
				}
			}
			if(command.equals("delete")){
				obj = objDAO.searchThongTinById(id);
				boolean check = objDAO.delete(obj);
				if(check){
					//JOptionPane.showMessageDialog(null, "Edit complete");
				}else{
					//JOptionPane.showMessageDialog(null, "Edit fail");
				}
			}
			if(command.equals("getMap")){
				PortletSession portletsession = request.getPortletSession();
				portletsession.setAttribute("Longlat", longitude +","+ lat,PortletSession.APPLICATION_SCOPE);
			}
			
			
		}
	}

	/**
	 * Get SessionBean.
	 * 
	 * @param request PortletRequest
	 * @return Thongtin_tiepnhanPortletSessionBean
	 */
	private static Thongtin_tiepnhanPortletSessionBean getSessionBean(PortletRequest request) {
		PortletSession session = request.getPortletSession();
		if( session == null )
			return null;
		Thongtin_tiepnhanPortletSessionBean sessionBean = (Thongtin_tiepnhanPortletSessionBean)session.getAttribute(SESSION_BEAN);
		if( sessionBean == null ) {
			sessionBean = new Thongtin_tiepnhanPortletSessionBean();
			session.setAttribute(SESSION_BEAN,sessionBean);
		}
		return sessionBean;
	}

	/**
	 * Returns JSP file path.
	 * 
	 * @param request Render request
	 * @param jspFile JSP file name
	 * @return JSP file path
	 */
	private static String getJspFilePath(RenderRequest request, String jspFile) {
		String markup = request.getProperty("wps.markup");
		if( markup == null )
			markup = getMarkup(request.getResponseContentType());
		return JSP_FOLDER + markup + "/" + jspFile + "." + getJspExtension(markup);
	}

	/**
	 * Convert MIME type to markup name.
	 * 
	 * @param contentType MIME type
	 * @return Markup name
	 */
	private static String getMarkup(String contentType) {
		if( "text/vnd.wap.wml".equals(contentType) )
			return "wml";
        else
            return "html";
	}

	/**
	 * Returns the file extension for the JSP file
	 * 
	 * @param markupName Markup name
	 * @return JSP extension
	 */
	private static String getJspExtension(String markupName) {
		return "jsp";
	}
	private Date convertStringToDate(String str){
		Date date = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			date = sdf.parse(str);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 return date;
	}
	private Date convertStringToTime(String str){
		Date date = null;
		SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss");
		try {
			date = sdf.parse(str);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 return date;
	}

}
